package com.bosssoft.goods.service.impl;

import com.bosssoft.goods.dao.SpecAttrDao;
import com.bosssoft.goods.domain.bean.SpecProp;
import com.bosssoft.goods.domain.vo.AddSpecVO;
import com.bosssoft.goods.domain.vo.EditSpecVO;
import com.bosssoft.goods.service.GoodsSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类商品参数管理服务接口类实现类
 * @author Fang_Sir
 * @date 2021-04-17
 */
@Service
public class GoodsSpecificationServiceImpl implements GoodsSpecificationService {
    @Autowired
    private SpecAttrDao specAttrDao;

    @Override
    public List<SpecProp> getCatSpec(int catId) {
        return specAttrDao.getSpecByCatId(catId);
    }

    @Override
    public SpecProp addSpec(AddSpecVO spec) throws Exception{
        SpecProp result = specAttrDao.getSpecInfoByName(spec.getCatId(),spec.getName());
        if(result != null) {
            throw new Exception("该参数名称已存在");
        }

        specAttrDao.addSpec(spec);
        return specAttrDao.getSpecInfoByName(spec.getCatId(),spec.getName());
    }

    @Override
    public boolean editSpec(EditSpecVO spec) throws Exception {
        SpecProp result = specAttrDao.getSpecInfoByName(spec.getCatId(),spec.getName());
        if(result != null) {
            throw new Exception("该参数名称已存在");
        }
        return specAttrDao.updateSpecName(spec.getPropId(), spec.getCatId(), spec.getName()) > 0;
    }

    @Override
    public boolean delSpec(int propId) {
        return specAttrDao.delSpec(propId) > 0;
    }
}
